library(algselbench)
library(devtools)
library(ggplot2)
load_all("~/work/coseal/aslib-r/aslib/")

# maxsat = parseASScenario("/home/bischl/cos/coseal/data/MAXSAT12-PMS")
# qbf = parseASScenario("/home/bischl/cos/coseal/data/QBF-2011")
maxsat = parseASScenario("/tmp/aslib_data-aslib-v1.0.1/MAXSAT12-PMS")
qbf = parseASScenario("/tmp/aslib_data-aslib-v1.0.1/QBF-2011")

# pdf("pics/box_maxsat.pdf")
# print(plotAlgoPerfBoxplots(maxsat, impute.zero.vals = TRUE, log = F))
# dev.off()
# 
# pdf("pics/dens_qbf.pdf")
# print(plotAlgoPerfDensities(qbf, impute.zero.vals = TRUE, log = TRUE))
# dev.off()
# 
# pdf("pics/prob_qbf.pdf")
# print(plotAlgoPerfCDFs(qbf, impute.zero.vals = TRUE, log = TRUE))
# dev.off()
# 
# pdf("pics/scatter_qbf.pdf")
# print(plotAlgoPerfScatterMatrix(qbf, impute.zero.vals = TRUE, log = TRUE))
# dev.off()


#############################################################################

## Modify figures for better visualization within the paper:

## Creating some plots manually for the paper:
  asscenario = qbf
  measure = qbf$desc$performance_measures
  impute.zero.vals = TRUE
  log = TRUE

## QBF-Boxplot:
  z = getEDAAlgoPerf(asscenario, measure, jitter = TRUE, impute.failed.runs = TRUE,
    impute.zero.vals = impute.zero.vals, check.log = log, 
    format = "long", with.instance.id = FALSE)
  p.box = ggplot(z$data, aes_string(x = "algorithm", y = z$measure, col = "algorithm")) +
    geom_boxplot() +
    theme(
      axis.title.x = element_blank(),
      axis.text.x = element_text(angle = 90, vjust = 1)
    ) + scale_y_log10()

## QBF-Density:
  z = getEDAAlgoPerf(asscenario, measure, impute.failed.runs = TRUE, jitter = FALSE,
    impute.zero.vals = impute.zero.vals, check.log = log, format = "long", with.instance.id = FALSE)
  z$data = z$data[!is.na(z$data[,z$measure]), ]
  p.dens = ggplot(z$data, aes_string(x = z$measure, col = "algorithm")) + 
    geom_density(na.rm = TRUE) + 
    coord_cartesian(xlim = c(z$range[1] + 1e-6, z$range[2])) + 
    scale_x_log10()

## QBF-CDF:
  z = getEDAAlgoPerf(asscenario, measure, impute.failed.runs = TRUE, jitter = FALSE,
    impute.zero.vals = impute.zero.vals, check.log = log, format = "long", with.instance.id = FALSE)
  # only plot area where we have successful runs
  p.cdf = ggplot(z$data, aes_string(x = z$measure, col = "algorithm")) + 
    stat_ecdf() +
    coord_cartesian(xlim = c(z$range[1] + 1e-6, z$range[2])) + 
    scale_x_log10() +
    scale_y_continuous("cumulative density")


# Extract Legend 
g_legend = function(a.gplot){ 
  tmp = ggplot_gtable(ggplot_build(a.gplot)) 
  leg = which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
  legend = tmp$grobs[[leg]] 
  return(legend)
}

library(gridExtra)
# pdf("pics/qbf_3plots.pdf", width = 7, height = 3.5)
#   grid.arrange(
#     p.box + theme(legend.position = "none") + scale_x_discrete(labels=rep("", 5)) + 
#       theme(plot.margin = unit(c(0, 0.5, -0.5, 0), "lines")),
#     p.dens + theme(legend.position = "none") + theme(plot.margin = unit(c(0, 0.5, 0, 0), "lines")),
#     p.cdf + theme(legend.position = "none") + theme(plot.margin = unit(c(0, 0.5, 0, 0), "lines")),
#     nrow=1)
# dev.off()

pdf("pics/qbf_cdf.pdf", width = 6, height = 3.5)
  p.cdf + #theme(legend.position = "none") + 
  theme(plot.margin = unit(c(0, 0.5, 0, 0), "lines"))
dev.off()

# pdf("pics/qbf_2plots.pdf", width = 5.5, height = 3.5)
# grid.arrange(
#   p.dens + theme(legend.position = "none") + theme(plot.margin = unit(c(0, 0.5, 0, 0), "lines")),
#   p.cdf + theme(legend.position = "none") + theme(plot.margin = unit(c(0, 0.5, 0, 0), "lines")),
#   nrow=1)
# dev.off()

pdf("pics/qbf_boxplot.pdf", width = 2.5, height = 3.5)
p.box + theme(legend.position = "none") + scale_x_discrete(labels=rep("", 5)) + 
  theme(plot.margin = unit(c(0, 0, -0.5, 0), "lines"))
dev.off()

pdf("pics/qbf_legend.pdf", width = 2.5, height = 3)
grid.arrange(g_legend(p.dens), nrow = 1)
dev.off()

########################

pdf("pics/qbf_scatter.pdf", width = 5, height = 5)
  plotAlgoPerfScatterMatrix(qbf, impute.zero.vals = TRUE, log = TRUE)
#   z = getEDAAlgoPerf(asscenario, measure, impute.failed.runs = TRUE, jitter = TRUE,
#     impute.zero.vals = impute.zero.vals, check.log = log, format = "wide", with.instance.id = FALSE)
#   pairs(z$data, log = ifelse(log, "xy", ""), xlim = range(z$data), ylim = range(z$data))
dev.off()

########################

pdf("pics/qbf_cormatrix.pdf", width = 4.5, height = 4.5)
  plotAlgoCorMatrix(qbf)
dev.off()
